// pages/operation/ruleSettings/addHoliday/index.js
import { timestampToTime } from "../../../../utils/index.js";
import Toast from "@vant/weapp/toast/toast";
import formRules from "../../../../utils/rules-form.js";
import { Holiday } from "../../../../api/api-work/holiday.js";
import Dialog from "@vant/weapp/dialog/dialog";
const holiday = new Holiday();

const rules = {
  name: [
    {
      required: true,
      message: "请输入名称",
    },
  ],
  startDate: [
    {
      required: true,
      message: "请选择开始日期",
    },
  ],
  endDate: [
    {
      required: true,
      message: "请选择结束日期",
    },
  ],
};
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    name: null,
    startDate: null,
    endDate: null,
    remark: null,
    showStartDate: false,
    showEndDate: false,
    minDate: new Date(2020, 0, 1).getTime(),
    currentStartDate: new Date().getTime(),
    currentEndDate: new Date().getTime(),
    state: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.form) {
      const form = JSON.parse(options.form); //传递参数
      let { id, name, startDate, endDate, remark, state } = form;
      this.setData({
        id,
        name,
        startDate,
        endDate,
        remark,
        state,
        currentStartDate: new Date(startDate).getTime(),
        currentEndDate: new Date(endDate).getTime(),
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},

  onChangeState() {
    this.setData({
      state: this.data.state === 0 ? 1 : 0, //cuow
    });
  },

  // 开始日期
  showStartDatePopup() {
    this.setData({
      showStartDate: true,
    });
  },
  onCloseStartDate() {
    this.setData({
      currentStartDate: this.data.startDate
        ? new Date(this.data.startDate).getTime()
        : new Date().getTime(),
      showStartDate: false,
    });
  },
  chooseStartDate(e) {
    if (this.data.endDate) {
      if (
        new Date(timestampToTime(e.detail)).getTime() >
        new Date(this.data.endDate).getTime()
      ) {
        Toast("开始日期不能大于结束日期");
        return;
      }
    }
    this.setData({
      startDate: timestampToTime(e.detail),
      currentStartDate: new Date(timestampToTime(e.detail)).getTime(),
      showStartDate: false,
    });
    console.log(new Date(timestampToTime(e.detail)).getTime());
  },

  // 结束日期
  showEndDatePopup() {
    this.setData({
      showEndDate: true,
    });
  },
  onCloseEndDate() {
    this.setData({
      currentStartDate: this.data.endDate
        ? new Date(this.data.endDate).getTime()
        : new Date().getTime(),
      showEndDate: false,
    });
  },
  chooseEndDate(e) {
    if (this.data.startDate) {
      if (
        new Date(this.data.startDate).getTime() >
        new Date(timestampToTime(e.detail)).getTime()
      ) {
        Toast("结束日期不能小于开始日期");
        return;
      }
    }
    this.setData({
      endDate: timestampToTime(e.detail),
      currentEndDate: new Date(timestampToTime(e.detail)).getTime(),
      showEndDate: false,
    });
  },

  addHoliday() {
    const params = {
      id: this.data.id,
      name: this.data.name,
      startDate: this.data.startDate, // 开始日期
      endDate: this.data.endDate, // 结束日期
      remark: this.data.remark, // 备注
      state: this.data.state,
    };
    formRules(rules, this.data, (status) => {
      if (status) {
        if (this.data.id == undefined || this.data.id == "") {
          // 新增保存
          holiday.saveHolidayAPI(params).then((res) => {
            wx.navigateBack();
          });
        } else {
          holiday.updateHolidayAPI(params).then((res) => {
            wx.navigateBack();
          });
        }
      }
    });
  },

  deleteHoliday() {
    Dialog.confirm({
      title: "你确定要删除吗?",
    })
      .then(() => {
        holiday.deleteHolidayAPI({ id: this.data.id }).then((res) => {
          wx.navigateBack();
        });
      })
      .catch(() => {
        // on cancel
      });
  },
});
